﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Validation.sp2010.Classes;
using Validation.sp2010.ControlTemplates.Validation.sp2010;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;

namespace Validation.sp2010.CONTROLTEMPLATES.Validation.sp2010
{
    public partial class CreateTask : UserControl
    {

        #region constante

        const string stateIdStep = "IdStep";
        const string stateIsNew = "IsNew";
        
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Load the field and the user profile Properties to the controls
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="userProfileProperties"></param>
        public void LoadData(List<SPField> fields, List<SPField> userFields, List<CoreProperty> userProfileProperties)
        {

            (Actor_Task as GetUser).LoadData(userFields, userProfileProperties);
            (TextBox_Title_Task as FormulaText).LoadData(fields,"");
            (TextBox_Desc_Task as FormulaText).LoadData(fields,"");

            ViewState[stateIsNew] = true;
            this.DataBind();
        }

        /// <summary>
        /// 
        ///to edit 
        /// </summary>
        /// <param name="step"></param>
        public void LoadStep(StepWF step, List<SPField> fields, List<SPField> userFields, List<CoreProperty> userProfileProperties)
        {
            TextBox_TitleStep_Task.Text = step.Title;
            
            (Actor_Task as GetUser).LoadData(userFields, userProfileProperties, step.Actor);

            DropDown_NotifyUser.SelectedIndex = step.NotifyUserByMail ? 0 : 1;

            (TextBox_Title_Task as FormulaText).LoadData(fields, step.ObjectDisplay);
            (TextBox_Desc_Task as FormulaText).LoadData(fields, step.BodyDisplay);
            

            DropDown_Extension_Task.SelectedIndex = step.WithExtension ? 0 : 1;
            DropDown_ProlongedExtension.SelectedIndex = step.ProlongedExtension ? 0 : 1;

            DropDown_ForceValidation.SelectedIndex=step.ForceValidation?0:1;

            DropDown_StartStep.SelectedIndex = step.IsInitial ? 0 : 1;
            DropDown_FinalStep.SelectedIndex = step.IsFinal ? 0 : 1;

            ViewState[stateIsNew] = false;
            ViewState[stateIdStep] = step.Sequence;
            this.DataBind();
        }

        /// <summary>
        /// Return the Current Task created null if error
        /// </summary>
        /// <returns></returns>
        public StepWF GetTaskStep()
        {
            StepWF stepWf = new StepWF();

            if (!(bool)ViewState[stateIsNew])
            {
                stepWf.Sequence = (int)ViewState[stateIdStep];
            }

            FormulaText titleTask = (TextBox_Title_Task as FormulaText);


            FormulaText desc = (TextBox_Desc_Task as FormulaText);
            GetUser actor = (Actor_Task as GetUser);

            StepPrincipal principal = actor.GetPrincipal();
            if (principal == null)
            {
                return null;
            }


            if (!titleTask.VerifyFormule())
                return null;

            if (!desc.VerifyFormule())
                return null;




            stepWf.ObjectDisplay = titleTask.DisplayFinalString;
            stepWf.Object = titleTask.InternalFinalString;

            stepWf.Title = TextBox_TitleStep_Task.Text;

            stepWf.Actor = actor.GetPrincipal();
            stepWf.NotifyUserByMail = DropDown_NotifyUser.SelectedIndex == 0;
            stepWf.Body = desc.InternalFinalString;
            stepWf.BodyDisplay = desc.DisplayFinalString;
            stepWf.TypeStep = TypeStep.Task;
            stepWf.WithExtension = DropDown_Extension_Task.SelectedIndex == 0;
            stepWf.ProlongedExtension = DropDown_ProlongedExtension.SelectedIndex == 0;

            stepWf.ForceValidation = DropDown_ForceValidation.SelectedIndex == 0;

            stepWf.IsInitial = DropDown_StartStep.SelectedIndex == 0;
            stepWf.IsFinal = DropDown_FinalStep.SelectedIndex == 0;
            return stepWf;
        }
    }
}
